using System;
using Math;

namespace Math
{
    public class ComplexInterval : ICloneable
    {
        public ComplexInterval()
        {
            re_ = new Interval();
            im_ = new Interval();
        }

        public ComplexInterval(Math.Interval real, Math.Interval imag)
        {
            re_ = (Math.Interval)real.Clone();
            im_ = (Math.Interval)imag.Clone();
        }

        // Accessor prototype matches STL complex template
        public Math.Interval real() { return re_; }
        public Math.Interval imag() { return im_; }

        public static ComplexInterval operator +(ComplexInterval lhs, ComplexInterval rhs)
        {
            return new ComplexInterval(lhs.re_ + rhs.re_, lhs.im_ + rhs.im_);
        }
        public static ComplexInterval operator -(ComplexInterval lhs, ComplexInterval rhs)
        {   
            return new ComplexInterval(lhs.re_ - rhs.re_, lhs.im_ - rhs.im_);
        }
        public override string ToString()
        {
            return (String.Format("{0} + {1}i)", re_, im_));
        }

        public static ComplexInterval square(ComplexInterval lhs)
        {
            return new ComplexInterval(
                Math.Interval.square(lhs.re_) - Math.Interval.square(lhs.im_),
                2.0 * lhs.re_ * lhs.im_);
        }
        
        public static bool subset(ComplexInterval lhs, ComplexInterval rhs)
        {
            return Math.Interval.subset(lhs.real(), rhs.real()) && Math.Interval.subset(lhs.imag(), rhs.imag());
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        // Member variables
        private Math.Interval re_;
        private Math.Interval im_;
    }
}
  